package com.example.home.attorney.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.attorney.activity.ConsultActivity;
import com.example.home.attorney.bean.InitLawyerListBean;
import com.google.gson.Gson;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class InitLawyerListAdapter extends RecyclerView.Adapter<InitLawyerListAdapter.MyHoldy> {
    private Context context;
    private List<InitLawyerListBean.RowsBean> list;

    public InitLawyerListAdapter(Context context, List<InitLawyerListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_hatt, null);
        return new InitLawyerListAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        Glide.with(context).load(ip+list.get(position).getAvatarUrl()).into(holder.imgUrl);
        holder.name.setText(list.get(position).getName());
        holder.workStartAt.setText("从"+list.get(position).getWorkStartAt()+"执业至今");
        holder.serviceTimes.setText("咨询人数："+list.get(position).getServiceTimes());
        holder.legalExpertiseName.setText("擅长："+list.get(position).getLegalExpertiseName());
        holder.favorableRate.setText("好评率:"+list.get(position).getFavorableRate()+"%");
        holder.consult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ConsultActivity.class);
                Gson gson = new Gson();
                intent.putExtra("conuslt",gson.toJson(list.get(position)));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView imgUrl;
        private TextView name;
        private TextView workStartAt;
        private TextView serviceTimes;
        private TextView legalExpertiseName;
        private TextView favorableRate;
        private Button consult;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            workStartAt = itemView.findViewById(R.id.workStartAt);
            serviceTimes = itemView.findViewById(R.id.serviceTimes);
            legalExpertiseName = itemView.findViewById(R.id.legalExpertiseName);
            favorableRate = itemView.findViewById(R.id.favorableRate);
            consult = itemView.findViewById(R.id.consult);
        }
    }
}
